package com.haotunet.app.goldtreasure.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.GCoinAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.CoinInfo;
import com.haotunet.app.goldtreasure.model.CoinModel;
import com.haotunet.app.goldtreasure.model.SignModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class GCoinDetailActivity extends BaseActivity implements OnClickListener {
    @InjectView(R.id.title_view)
    TitleView titleView;
    @InjectView(R.id.iv_no_coin)
    ImageView mIvNoCoin;
    @InjectView(R.id.id_swipe_ly)
    PullToRefreshLayout idSwipeLy;
    @InjectView(R.id.lv_gCoin)
    PullableListView lvGCoin;
    private TextView txtGoldcoinNum;
    private TextView txtSign;
    private int pageNum = 20;
    private final int GET_COIN_INFO_SUCCESS = 2;
    private final int LOAD_MORE_COIN_INFO_SUCCESS = 3;
    private Handler handle = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case GET_COIN_INFO_SUCCESS:
                    CoinModel coinModel = (CoinModel) msg.obj;
                    txtSign.setText(coinModel.getIsSignIn() ? getString(R.string.sign_in) : getString(R.string.sign));
                    txtSign.setEnabled(!coinModel.getIsSignIn());
                    List<CoinInfo> coinList = coinModel.getItems();
                    if (coinList.size() == 0) {
                        mIvNoCoin.setVisibility(View.VISIBLE);
                        return;
                    }
                    mIvNoCoin.setVisibility(View.GONE);
                    gCoinAdapter.setDatas(coinList);

                    break;
                case LOAD_MORE_COIN_INFO_SUCCESS:
                    CoinModel moreModel = (CoinModel) msg.obj;
                    if (moreModel.getItems().size() == 0) {
                        UIHelper.showToast(GCoinDetailActivity.this, getString(R.string.last_page_hint));
                    }
                    gCoinAdapter.addDatas(moreModel.getItems());
                    break;
                default:
                    break;
            }
        }

    };
    private GCoinAdapter gCoinAdapter;
    private ActionUtil actionUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gcoin_detail);
        ButterKnife.inject(this);
        initView();
        getGoldCoinList("0", "");
    }

    private void getGoldCoinList(String pagenum, final String action) {
        actionUtil = new ActionUtil(GCoinDetailActivity.this);
        actionUtil.getGoldCoinInfo(pagenum);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                CoinModel coinModel = (CoinModel) model;
                if (coinModel == null) {
                    UIHelper.showToast(GCoinDetailActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (coinModel.getErrorMsg() != null) {
                    if (coinModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
                        ActivityUtils.forceQuit(GCoinDetailActivity.this, getString(R.string.crowding_in_tips_txt));
                    } else {
                        UIHelper.showToast(GCoinDetailActivity.this, coinModel.getErrorMsg());
                    }
                } else {
                    if (action.equals("refresh") || action.equals("loadmore")) {
                        idSwipeLy.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                    Message msg = new Message();
                    if (action.equals("loadmore")) {
                        msg.what = LOAD_MORE_COIN_INFO_SUCCESS;
                        pageNum = pageNum + 20;
                    } else {
                        msg.what = GET_COIN_INFO_SUCCESS;
                    }
                    msg.obj = coinModel;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void initView() {
        String coinNum = getIntent().getStringExtra("coinNum");
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getTitletextView().setText(getString(R.string.gold_coin));
        titleView.getBackView().setVisibility(View.VISIBLE);
        titleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        View headView = LayoutInflater.from(GCoinDetailActivity.this).inflate(R.layout.headview_list_coin, null);
        txtGoldcoinNum = (TextView) headView.findViewById(R.id.txt_goldcoin_num);
        txtSign = (TextView) headView.findViewById(R.id.txt_sign);
        lvGCoin.addHeaderView(headView);
        gCoinAdapter = new GCoinAdapter(GCoinDetailActivity.this, null);
        lvGCoin.setAdapter(gCoinAdapter);
        txtGoldcoinNum.setText(coinNum);
        txtSign.setOnClickListener(this);
        RefreshData(0);
    }

    private void RefreshData(final int pagenum) {
        idSwipeLy.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                getGoldCoinList(pagenum + "", "refresh");
                pageNum = 20;
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                getGoldCoinList(pageNum + "", "loadmore");
            }
        });
    }

    private void everyday_sign() {
        ActionUtil actionUtil = new ActionUtil(GCoinDetailActivity.this);
        actionUtil.everyday_sign();
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                SignModel signModel = (SignModel) model;
                if (signModel == null) {
                    UIHelper.showToast(GCoinDetailActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (signModel.getErrorMsg() != null) {
                    UIHelper.check(GCoinDetailActivity.this, signModel.getErrorMsg());
                } else {
                    UIHelper.check(GCoinDetailActivity.this, signModel.getSignInMsg());
                    getGoldCoinList("0", "");
                    txtSign.setText(getString(R.string.sign_in));
                    txtSign.setEnabled(false);
                    String totalCoins = signModel.getTotalCoins();
                    txtGoldcoinNum.setText(totalCoins);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_sign:
                everyday_sign();
                break;
        }
    }
}
